#include "head.h"

int net_rm(request_t *request, int net_fd, MYSQL *mysql) {
    // 拼接相对路径
    char relative_path[200] = {0};
    strcpy(relative_path, request->path);
    if (strcmp(request->path, "/") == 0) {
        strcat(relative_path, request->args);
    } else {
        strcat(relative_path, "/");
        strcat(relative_path, request->args);
    }
    // printf("服务端拼接的相对路径为: %s\n", relative_path);
    LOG(INFO, request->user_name, "rm 服务端拼接的相对路径", relative_path);

    // 查询数据库表判断该文件是否存在
    int user_id = get_user_id(request->user_name, mysql);
    char sql[1024] = {0};
    sprintf(sql, "select id, type, delete_flag from files where path = '%s' and user_id = %d", relative_path, user_id);
    int ret = mysql_query(mysql, sql);
    if (ret != 0) {
        // printf("select: %s\n", mysql_error(mysql));
        LOG(ERROR, request->user_name, "rm select", mysql_error(mysql));
    }
    MYSQL_RES *result = mysql_store_result(mysql);
    if (result == NULL) {
        // printf("store: %s\n", mysql_error(mysql));
        LOG(ERROR, request->user_name, "rm store", mysql_error(mysql));
    }

    MYSQL_ROW row;
    if (!(row = mysql_fetch_row(result))) {
        // 文件不存在, 发送响应
        // printf("文件不存在\n");
        LOG(INFO, request->user_name, "rm", "文件不存在");
        response_t response;
        response.status = 1;
        strcpy(response.args, "没有那个文件");
        send(net_fd, &response, sizeof(response_t), 0);
        mysql_free_result(result);
        return 0;
    }

    // 若文件存在, 则获取该文件的id, type, delete_flag
    // int id, type, delete_flag;
    int id = atoi(row[0]);
    int type = atoi(row[1]);
    int delete_flag = atoi(row[2]);

    // 文件存在, 判断是否为目录, 目录则不能删除
    if (type == 0) {
        // 发送响应
        // printf("文件为目录, 不能删除\n");
        LOG(INFO, request->user_name, "rm", "文件为目录, 不能删除");
        response_t response;
        response.status = 1;
        strcpy(response.args, "file is directory, can't delete");
        send(net_fd, &response, sizeof(response_t), 0);
        mysql_free_result(result);
        return 0;
    }

    // 若删除标记位为0, 则已经删除
    if (delete_flag == 0) {
        // 发送响应
        // printf("文件已经删除\n");
        LOG(INFO, request->user_name, "rm", "文件已经删除");
        response_t response;
        response.status = 1;
        strcpy(response.args, "file already deleted");
        send(net_fd, &response, sizeof(response_t), 0);
        mysql_free_result(result);
        return 0;
    }
    mysql_free_result(result);

    // memset(sql, 0, sizeof(sql));
    char sql2[1024] = {0};
    sprintf(sql2, "select file_hash from files where id = %d", id);
    ret = mysql_query(mysql, sql2);
    if (ret != 0) {
        // printf("select: %s\n", mysql_error(mysql));
        LOG(ERROR, request->user_name, "rm select", mysql_error(mysql));
    }
    MYSQL_RES *result2 = mysql_store_result(mysql);
    if (result2 == NULL) {
        // printf("store: %s\n", mysql_error(mysql));
        LOG(ERROR, request->user_name, "rm store", mysql_error(mysql));
    }

    MYSQL_ROW row2;
    if (!(row2 = mysql_fetch_row(result2))) {
        // printf("file_hash not found\n");
        LOG(ERROR, request->user_name, "rm", "file_hash not found");
        response_t response;
        response.status = 1;
        strcpy(response.args, "delete file failed");
        send(net_fd, &response, sizeof(response_t), 0);
        mysql_free_result(result2);
        return 0;
    }
    char file_hash[200] = {0};
    strcpy(file_hash, row2[0]);
    mysql_free_result(result2);

    memset(sql, 0, sizeof(sql));
    // 更新文件id对应的删除标记位
    sprintf(sql, "update files set delete_flag = 0 where id = %d", id);
    ret = mysql_query(mysql, sql);
    if (ret != 0) {
        // printf("update: %s\n", mysql_error(mysql));
        LOG(ERROR, request->user_name, "rm update", mysql_error(mysql));
    }

    MYSQL_RES *result3 = mysql_store_result(mysql);
    if (result3 == NULL) {
        // printf("store: %s\n", mysql_error(mysql));
        LOG(ERROR, request->user_name, "rm store", mysql_error(mysql));
    }
    // memset(row, 0, sizeof(MYSQL_ROW));
    // MYSQL_ROW row3;

    // if (!(row3 = mysql_fetch_row(result3))) {
    //     printf("update: %s\n", mysql_error(mysql));
    // }
    // printf("更新了文件id对应的删除标记位\n");
    LOG(INFO, request->user_name, "rm", "更新了文件id对应的删除标记位");
    mysql_free_result(result3);

    // 判断是否需要执行rm命令
    char ssql[1024] = {0};
    sprintf(ssql, "select id from files where filename = '%s' and delete_flag = 1", request->args);
    ret = mysql_query(mysql, ssql);
    if (ret != 0) {
        // printf("select: %s\n", mysql_error(mysql));
        LOG(ERROR, request->user_name, "rm select", mysql_error(mysql));
    }

    MYSQL_RES *result1 = mysql_store_result(mysql);
    if (result1 == NULL) {
        // printf("store: %s\n", mysql_error(mysql));
        LOG(ERROR, request->user_name, "rm store", mysql_error(mysql));
    }
    // printf("result is not null\n");
    LOG(INFO, request->user_name, "rm", "result is not null");
    memset(row, 0, sizeof(MYSQL_ROW));
    if (!(row = mysql_fetch_row(result1))) {
        // printf("file_id not found\n");
        LOG(INFO, request->user_name, "rm", "file_id not found");
        char rm[1024] = "rm ";
        strcat(rm, file_hash);

        if (system(rm) != 0) {
            // printf("删除文件失败\n");
            LOG(ERROR, request->user_name, "rm", "删除文件失败");
            response_t response;
            response.status = 1;
            strcpy(response.args, "delete file failed");
            send(net_fd, &response, sizeof(response_t), 0);
            mysql_free_result(result1);
            return 0;
        }
    }

    // 发送响应
    response_t response;
    response.status = 0;
    strcpy(response.args, "delete file success");
    send(net_fd, &response, sizeof(response_t), 0);

    mysql_free_result(result1);
    return 0;
}